<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use App\Services\MaterialService;
use App\Services\CommonService;
use App\Services\OSS;
use Intervention\Image\ImageManagerStatic as Image;
use App\Services\CourseTypeService;

class MaterialController extends Controller{
	public function uploads(Request $request){
		$file = $request->file('material_file');
		if(!$file->isValid()){
			$data = array('code'=>400);
		}else{
			// 获取扩展名
			$ext = $file->getClientOriginalExtension();

			// 生成新文件名
			$newfile = md5(time().rand(1000,9999)).'.'.$ext;

			// 文件上传指定路径
			$path = public_path('banner');

			$target_file = 'banner/'.$newfile;

			// 将上传的文件移动到制定目录
			/*if($file->move($path,$newfile)){
				$data = array('code'=>200,'imgurl'=>$newfile);
			}*/

			/*$res = Image::make($file);->resize(300,200)->save($path.'/'.$newfile);
			if($res){
				$data = array('code'=>200,'imgurl'=>$newfile);
			}*/
			// $osskey 文件上传到oss后的新文件名
			// $filepath 要上传的文件真实资源
			$res = OSS::upload($target_file,$file->getRealPath());

			if($res){
				$data = array('code'=>200,'imgurl' => $target_file);
			}else{
				$data = array('code'=>400);
			}

		}

		return response()->json($data);
	}

	public function list(Request $request){
		$username = Session::get('username');

		if(!$username){
			return redirect('/admin/login'); 	
		}else{
			$data['page_size_list'] = array(20,25,50,100);
			$data['page_size'] = $request->input('page_size') ? intval($request->input('page_size')) : 15;
			$data['keywords'] = $request->input('keywords') ? addslashes($request->input('keywords')) : '';

			$material = new MaterialService;
			$data['username'] = $username;
			$data['mlist'] = $material->getMaterialList($data['page_size'],$data['keywords']);
		}

		return view('admin.material.list',$data);
	}

	public function create(){
		$username = Session::get('username');

		if(!$username){
			return redirect('/admin/login'); 	
		}else{
		    $courseType = new CourseTypeService;

		    $data['ctlist'] = $courseType->getTypeList();
			$data['username'] = $username;
			$data['timestamp'] = time();
			$data['token'] = md5('unique_salt' . $data['timestamp']);
		}

		return view('admin.material.add',$data);
	}

	public function store(Request $request){

		$material = new MaterialService;

		date_default_timezone_set('PRC');

		$data['banner_name'] = addslashes($_POST['material_name']);
		$data['banner_img'] = addslashes($_POST['material_imgurl']);
		$data['target_link'] = addslashes($_POST['target_link']);
		$data['position'] = intval($_POST['position']);
		$data['add_date'] = date('Y-m-d H:i:s');
		$data['update_date'] = date('Y-m-d H:i:s');
		$data['isshow'] = 1;

		$res = $material->insertMaterial($data);
		if($res){
			$_data['code'] = 200;
		}else{
			$_data['code'] = 400;
		}

		return response()->json($_data);
	}

	public function edit($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login'); 	
		}else{
			$material = new MaterialService;
			$courseType = new CourseTypeService;

			$data['ctlist'] = $courseType->getTypeList();
			$data['username'] = $username;
			$data['info'] = $material->getMaterialInfo($id);
		}

		return view('admin.material.edit',$data);

	}

	public function update(){
		$material = new MaterialService;

		$id = intval($_POST['id']);
		$data['banner_name'] = addslashes($_POST['material_name']);
		$data['banner_img'] = addslashes($_POST['material_imgurl']);
		$data['target_link'] = addslashes($_POST['target_link']);
		$data['position'] = intval($_POST['position']);
		$data['update_date'] = date('Y-m-d H:i:s');

		$res = $material->modifyMaterial($id,$data);

		if($res){
			$_data['code'] = 200;
		}else{
			$_data['code'] = 400;
		}

		return response()->json($_data);
	}

	public function del($id){
		$material = new MaterialService;
		
		$data['isshow'] = 0;
		$res = $material->modifyMaterial($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}
		return response()->json($_data);
	}

	public function grounding($id){
	    $material = new MaterialService;
	    $data['grounding'] = 'yes';

	    $res = $material->modifyMaterial($id,$data);
	    if($res){
	        $_data = array('code'=>200);
        }else{
	        $_data = array('code'=>400);
        }

	    return response()->json($_data);
    }

    public function undercarriage($id){
        $material = new MaterialService;
        $data['grounding'] = 'no';

        $res = $material->modifyMaterial($id,$data);
        if($res){
            $_data = array('code'=>200);
        }else{
            $_data = array('code'=>400);
        }

        return response()->json($_data);
    }
}